﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using RestSharp;
using RestSharp.Authenticators;
using XingNet.Web.Communication;
using XingNet.Web.Communication.Base;
using XingNet.Web.Model.v1_Users;
using Xunit;

namespace XingNet.Web.Communication.Base
{
  public class XingBaseClient
  {

    public static dynamic GetClient(XingNetTokenHandler _TokenHandler)
    {

      var client = new RestClient("https://api.xing.com")
       {
         Authenticator = OAuth1Authenticator.ForProtectedResource(
                                                            _TokenHandler.ConsumerKey,
                                                            _TokenHandler.ConsumerSecret,
                                                            _TokenHandler.Access_Token,
                                                            _TokenHandler.Access_Token_Secret)
       };

      return client;
    }

    public enum RequestMode
    {
      RequestToken = 0,
      AuthorizeToken = 1,
      AccessToken = 2,
      ProtectedData = 3,
      UserLogin = 4      
    }
    public static dynamic GetClient(XingNetTokenHandler _TokenHandler, string callbackUrl,RequestMode Mode)
    {
      var client = new RestClient("https://api.xing.com");
     

      switch (Mode)
      {
        case RequestMode.RequestToken:
            client.Authenticator = OAuth1Authenticator.ForRequestToken(
                                                          _TokenHandler.ConsumerKey,
                                                          _TokenHandler.ConsumerSecret,
                                                          callbackUrl);
            break;
        case RequestMode.AuthorizeToken:
            client.Authenticator = OAuth1Authenticator.ForAccessToken(_TokenHandler.ConsumerKey,
                                                        _TokenHandler.ConsumerSecret,
                                                        _TokenHandler.Access_Token,
                                                        _TokenHandler.Access_Token_Secret,
                                                        _TokenHandler.Verifier);

           break;

        case RequestMode.AccessToken:
           client.Authenticator = OAuth1Authenticator.ForAccessToken(_TokenHandler.ConsumerKey,
                                             _TokenHandler.ConsumerSecret,
                                             _TokenHandler.Access_Token,
                                             _TokenHandler.Access_Token_Secret,
                                             _TokenHandler.Verifier);
           break;

        case RequestMode.ProtectedData:
           client.Authenticator = new HttpBasicAuthenticator(_TokenHandler.ConsumerKey, _TokenHandler.Access_Token);

           client.Authenticator = OAuth1Authenticator.ForProtectedResource(_TokenHandler.ConsumerKey,
                                   _TokenHandler.ConsumerSecret,
                                   _TokenHandler.Access_Token,
                                   _TokenHandler.Access_Token_Secret);
         
           client.FollowRedirects = true;
           break;
        case RequestMode.UserLogin:
           client.Authenticator = OAuth1Authenticator.ForClientAuthentication(_TokenHandler.ConsumerKey,
                                             _TokenHandler.ConsumerSecret,
                                             _TokenHandler.UserName,
                                             _TokenHandler.Password);
          break;
      }
       return client;
    }


  }
}
